package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Override
    @Transactional
    public void add(CheckGroup checkGroup, Integer[] checkItemIds) {
        log.debug("Service: add: checkGroup ===> "+checkGroup);
        log.debug("Service: add: checkItemIds ===> "+checkItemIds);
        checkGroupDao.add(checkGroup);
        if (checkGroup.getId() != null && checkItemIds !=null && checkItemIds.length >0){
            for (Integer checkItemId : checkItemIds) {
                checkGroupDao.set(checkGroup.getId(),checkItemId);
            }
        }
    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<CheckGroup> page = checkGroupDao.findByCondition(queryPageBean.getQueryString());
        log.debug("Service: findByPage: page ===> "+page);
        return new PageResult(page.getTotal(),page);
    }

    @Override
    public void delById(Integer id) {
        long count = checkGroupDao.findCountByCheckGroupId(id);
        if (count > 0){
            throw new RuntimeException("此检查组被套餐所关联,不能被删除!!!");
        }
        checkGroupDao.delRelation(id);
        checkGroupDao.delById(id);
    }

    @Override
    public CheckGroup findById(Integer id) {
        CheckGroup checkGroup = checkGroupDao.findById(id);
        log.debug("Service: findById: checkGroup ===> "+checkGroup);
        return checkGroup;
    }

    @Override
    public List<Integer> findCheckItemIdsById(Integer id) {
        List<Integer> checkItemIdsList = checkGroupDao.findCheckItemIdsById(id);
        log.debug("Service: findCheckItemIdsById: checkItemIdsList ===> "+checkItemIdsList);
        return checkItemIdsList;
    }

    @Override
    @Transactional
    public void edit(CheckGroup checkGroup, Integer[] checkItemIds) {
        log.debug("Service: edit: checkGroup ===> "+checkGroup);
        log.debug("Service: edit: checkItemIds ===> "+checkItemIds);
        checkGroupDao.edit(checkGroup);
        checkGroupDao.delRelation(checkGroup.getId());
        if (checkGroup.getId()!=null && checkItemIds!=null && checkItemIds.length>0){
            for (Integer checkItemId : checkItemIds) {
                checkGroupDao.set(checkGroup.getId(),checkItemId);
            }
        }

    }

    @Override
    public List<CheckGroup> findAll() {
        log.debug("Service: findAll: ===>");
        List<CheckGroup> checkGroupList = checkGroupDao.findAll();
        log.debug("Service: findAll: checkGroupList ===> " + checkGroupList);
        return checkGroupList;
    }
}
